package com.dinree.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.base.library.BaseFragment;
import com.dinree.R;
import com.dinree.activity.SearchActivity;
import com.dinree.bean.UserBean;
import com.dinree.databinding.FragmentClassBinding;
import com.dinree.utils.Constant;
import com.dinree.utils.LaunchUtil;
import com.jakewharton.rxbinding2.view.RxView;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.base.library.Constant.WINDOW_DURATION;

/**
 * @author 谢铖志 on 2017/12/8 16:01
 * @version V1.0
 * @modificationHistory
 * @modify by user: 谢铖志 on 2017/12/8 16:01
 */

public class ClassFragment extends BaseFragment {
    private FragmentClassBinding fragmentClassBinding;
    private UserBean userBean;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_class;
    }

    @Override
    public void initData(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
        fragmentClassBinding = getViewDataBinding();
        userBean = realm.findFirst(UserBean.class);
        if (userBean != null) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("user_id", userBean.user_id);
            map.put("user_token", userBean.user_token);
            fragmentClassBinding.webView.loadUrl(Constant.WEB_HOST + "front/#/category", map);
        } else {
            fragmentClassBinding.webView.loadUrl(Constant.WEB_HOST + "front/#/category");
        }
        RxView.clicks(fragmentClassBinding.message).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            LaunchUtil.launchActivityByUrl(mContext, Constant.WEB_HOST + "front/#/anncmt");
        });
        RxView.clicks(fragmentClassBinding.btnSearch).throttleFirst(WINDOW_DURATION, TimeUnit.SECONDS).subscribe(v -> {
            Intent intent = new Intent(mContext, SearchActivity.class);
            intent.putExtra("url", Constant.WEB_HOST + "front/#/search");
            startActivity(intent);
        });
        updatePage();
    }

    public void updatePage() {
        if (fragmentClassBinding != null) {
            fragmentClassBinding.webView.loadUrl("javascript:updatePage()");
        }
    }
}
